package com.qinker.oa.controller;

import com.qinker.oa.message.CodeMsg;
import com.qinker.oa.message.Result;
import com.qinker.oa.model.User;
import com.qinker.oa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result<CodeMsg> login(@Valid User user, HttpSession session){
        User userLogin = userService.userLogin(user);
        if (userLogin != null){
            session.setAttribute("user", userLogin);
            return Result.successNoData(CodeMsg.SUCCESS);
        }
        return Result.error(CodeMsg.PASSWORD_ERROR);
    }

    @RequestMapping("/session")
    public Result<User> getUserInfo(HttpSession session){
        User user = (User) session.getAttribute("user");
        return Result.success(user);
    }


}
